package com.audio.play;

import java.nio.ByteBuffer;
import java.nio.ShortBuffer;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

public class AudioPlayActivity extends Activity implements OnClickListener,
		OnSeekBarChangeListener {
	SeekBar slider;
	Button playButton;
	Button receiveButton;
	TextView display;
	boolean isPlaying = false;
	PlaySoundTask playSoundTask;
	AudioRecorder recorder;
	
	String result = "";
	
    Handler mHandler = new Handler(){
        @Override
            public void handleMessage(Message msg) {
               Log.i("TTSDeamon", "UPDATE_UI");
               display.setText( result );
        	}
    };
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		display = (TextView) findViewById(R.id.display);
		display.setText("5000 Hz");

		slider = (SeekBar) findViewById(R.id.slider);
		slider.setMax(20000);
		slider.setProgress(5000);
		slider.setOnSeekBarChangeListener(this);

		playButton = (Button) findViewById(R.id.play);
		playButton.setOnClickListener(this);
		
		receiveButton = (Button) findViewById(R.id.Receive);
		receiveButton.setOnClickListener(new OnClickListener() {  
            @Override  
            public void onClick(View v) {  
            	if ( recorder == null) {
            		receiveButton.setText( "Stop" );
	            	recorder = new AudioRecorder();
	            	recorder.init();
	            	recorder.start();  
            	} else {
            		receiveButton.setText( "Start" );
            		byte[] buffer = recorder.dout.toByteArray();
            		
            		SandBox sb = new SandBox();
        			ByteBuffer byteBuffer = ByteBuffer.wrap(buffer);
        			ShortBuffer shortBuffer = byteBuffer.asShortBuffer();
        			String result = sb.demodulateSampleBuffer( shortBuffer );
        			display.setText( result );
        			
            		recorder.free();
            		recorder = null;
            	}
            	
            }  
        });  

	}

	@Override
	public void onProgressChanged(SeekBar seekBar, int progress,
			boolean fromUser) {
		display.setText("" + progress + " Hz");
		playSoundTask.setFrequency( progress );
	}

	public void onClick(View v) {
		if (isPlaying) {
			stop();
		} else {
			start();
		}
	}

	public void stop() {
		isPlaying = false;
		playButton.setText( "Start" );
		playSoundTask.setPlaying( isPlaying );
	}

	public void start() {
		isPlaying = true;
		playButton.setText( "Stop" );
		playSoundTask = new PlaySoundTask();
		playSoundTask.setFrequency( slider.getProgress() );
		playSoundTask.setPlaying( isPlaying );
		playSoundTask.activity = this;
		playSoundTask.execute();
	}

	@Override
	protected void onResume() {
		super.onResume();

	}

	@Override
	protected void onStop() {
		super.onStop();
		// Store state
		stop();

	}

	@Override
	public void onStartTrackingTouch(SeekBar seekBar) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onStopTrackingTouch(SeekBar seekBar) {
		// TODO Auto-generated method stub

	}
}